package game.map.generator;

import game.graphics.Block;

import java.io.Serializable;
import java.util.Random;

import data.DataManager;

/**
 * Generate forest (few trees).
 * 
 * @author Tomas Zima
 *
 */
public class ForestGenerator implements Serializable
{
	/**
	 * Constructor.
	 * 
	 * @param blocks
	 */
	public ForestGenerator(Block blocks[][])
	{
		this.blocks			= blocks;
		
		this.mapWidth		= blocks.length;
		this.mapHeight		= blocks[0].length;
		this.treeGenerator	= new TreeGenerator(blocks);
	}
	
	/**
	 * Pregenerate world.
	 */
	public void pregenerate()
	{
		generate1();
	}
	
	/**
	 * Generate - 1st stage.
	 */
	private void generate1()
	{
		for (int i = 0; i < mapWidth - 3; i++)
		{
			for (int j = 1; j < mapHeight - 2; j++)
			{
				if (blocks[i][j + 1].getBlockType() == DataManager.BlockType.BLOCK_GRASS)
				{
					if (new Random().nextInt(30) == 0)
					{
						while (blocks[i += 3][j + 1].getBlockType() == DataManager.BlockType.BLOCK_GRASS && i < mapWidth - 3)
						{
							treeGenerator.createTree(i, j);
						}
					}
				}
			}
		}
	}
	
	private Block			blocks[][];
	private int				mapWidth;
	private int				mapHeight;
	private TreeGenerator	treeGenerator;
}
